package com.css.fxfzaqbz.modules.assess.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.CasAndUrgen;
import com.css.fxfzaqbz.modules.assess.entity.CasualtiesEntity;
import com.css.fxfzaqbz.modules.assess.repository.CasualtiesRepository;
import com.css.fxfzaqbz.modules.assess.vo.CasVo;
import com.css.fxfzaqbz.modules.assess2.entity.TaskContent;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-09-13 14:57
 */
@Repository
public class CasualtiesRepositoryImpl implements CasualtiesRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public CasualtiesEntity saveCasualties(CasualtiesEntity casualtiesEntity) {
        String sql = "insert into sys_casualties (sc_id,sc_ass_content,sc_date,sc_people,sc_file_id,sc_file_title,sc_file_describe,create_time,del_flag,create_user,sc_relation,types,mark,data_message) values(:scId,:scAssContent,:scDate,:scPeople,:scFileId,:scFileTitle,:scFileDescribe,:createTime,:delFlag,:createUser,:scRelation,:types,:mark,:dataMessage)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(casualtiesEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return casualtiesEntity;
    }

    @Override
    public List<CasualtiesEntity> getAllCasualties(String id) {
        String sql="";
        sql="select u.* from sys_casualties u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sc_relation='"+id+"' and u.mark='"+YNEnum.N.toString()+"' order by u.create_time desc";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(CasualtiesEntity.class));
    }

    @Override
    public CasualtiesEntity getCasualtiesById(String id) {
        String sql="";
        sql="select u.* from sys_casualties u  where  u.sc_id='"+id+"' and u.del_flag = '"+ YNEnum.N.toString() +"'";
        CasualtiesEntity casualtiesEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(CasualtiesEntity.class));
        return casualtiesEntity;
    }

    @Override
    public void deleteCasualties(String id, String userID, Timestamp currentTimestamp) {
        String sql="";
        sql="update sys_casualties  set del_flag ='"+YNEnum.Y.toString()+"',update_user='"+userID+"',update_time='"+currentTimestamp+"'where sc_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<CasualtiesEntity> getCasualtiesByType(String scType,String scRelation) {
        String sql="";
        sql="select u.* from sys_casualties u  where  u.sc_ass_content='"+scType+"' and u.sc_relation='"+scRelation+"' and u.del_flag = '"+ YNEnum.N.toString() +"'";

        try {
            List<CasualtiesEntity> casualtiesEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CasualtiesEntity.class));
            return casualtiesEntities;
        } catch (DataAccessException e) {
            return null;
        }

    }

    @Override
    public CasualtiesEntity updateCasualties(CasualtiesEntity casualtiesEntity, String scId) {
        String sql="";
        sql="update sys_casualties set sc_ass_content=:scAssContent ,sc_date=:scDate,sc_people=:scPeople,sc_file_id=:scFileId,sc_file_title=:scFileTitle,sc_file_describe=:scFileDescribe ,create_time=:createTime,create_user=:createUser where sc_id='"+scId+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(casualtiesEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return casualtiesEntity;
    }

    @Override
    public CasualtiesEntity updateCasualtiesById(CasualtiesEntity casualtiesEntity) {
        String sql="";
        sql="update sys_casualties set sc_ass_content=:scAssContent ,sc_date=:scDate,sc_people=:scPeople,sc_file_id=:scFileId,sc_file_title=:scFileTitle,sc_file_describe=:scFileDescribe ,update_time=:updateTime,update_user=:updateUser where sc_id='"+casualtiesEntity.getScId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(casualtiesEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return casualtiesEntity;
    }

    @Override
    public List<TaskContent> getAllCasualties1(String id) {
        String sql="";
        sql="select sc_id id,sc_ass_content as content,sc_date as dateTime from sys_casualties u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sc_relation='"+id+"' order by u.create_time desc";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(TaskContent.class));
    }

    @Override
    public List<CasualtiesEntity> getAllCasualtiesTwo(String id) {
        String sql="";
        sql="select u.* from sys_casualties u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sc_relation='"+id+"' and u.mark !='"+YNEnum.N.toString()+"' order by u.sc_ass_content,u.create_time desc";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(CasualtiesEntity.class));

    }

    @Override
    public void firstSubmitCasualties(String id, String userID, Timestamp currentTimestamp, String scFileDescribe) {
        String sql="";
        sql="update sys_casualties  set mark ='"+YNEnum.Y.toString()+"',update_user='"+userID+"',update_time='"+currentTimestamp+"',sc_file_describe='"+scFileDescribe+"' where sc_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void firstSubmitCasualties2(String id, String userID, Timestamp currentTimestamp) {
        String sql="";
        sql="update sys_casualties  set mark ='"+YNEnum.Y.toString()+"',update_user='"+userID+"',update_time='"+currentTimestamp+"' where sc_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public CasualtiesEntity casualtiesIfRepetition(CasualtiesEntity casualtiesEntity) {
        String sql="";
        sql="select u.* from sys_casualties u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sc_file_title='"+casualtiesEntity.getScFileTitle()+"' and u.sc_relation='"+casualtiesEntity.getScRelation()+"' and u.sc_ass_content ='"+casualtiesEntity.getScAssContent()+"'";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(CasualtiesEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void addCasualtiesDisaster(List<String> ids, String describe) {
        String sql = "update sys_casualties set sc_file_describe = '" + describe + "' " +
                " where sc_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public void removeCasualtiesDisaster(CasualtiesEntity casualtiesById) {
        String sql="";
        sql="update sys_casualties  set mark ='"+YNEnum.N.toString()+"',sc_file_describe='"+casualtiesById.getScFileDescribe()+"',submiter='"+casualtiesById.getSubmiter()+"' ,submit_phone='"+casualtiesById.getSubmitPhone()+"'  where sc_id = '"+casualtiesById.getScId()+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void secondSubmitCasualties(List<String> ids, String mobilePhone, String userName,Timestamp currentTimestamp) {
        String sql = "update sys_casualties set mark = '" + 2 + "',submiter='"+userName+"',submit_phone='"+mobilePhone+"',update_time='"+currentTimestamp+"',submit_time='"+currentTimestamp+"'" +
                " where sc_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public List<CasualtiesEntity> getCasualtiesByIaId(String id) {
        String sql = "select * from sys_casualties where sc_relation='" + id + "' and del_flag='" + YNEnum.N + "' and mark='" + 2 + "'";
        List<CasualtiesEntity> casualtiesEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CasualtiesEntity.class));
        return casualtiesEntities;
    }

    @Override
    public List getCasByIaIdAndType(String iaId, String rkfbt) {
        String sql = "select * from sys_casualties where sc_relation='" + iaId + "' and sc_ass_content='"+rkfbt+"' and del_flag='" + YNEnum.N + "' and mark='" + YNEnum.Y + "'";
        List<CasualtiesEntity> casualtiesEntities = null;
        try {
            casualtiesEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CasualtiesEntity.class));
            return casualtiesEntities;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public List<CasAndUrgen> getAll(CasVo vo) {
        String sql="select * from (select sc_id id, sc_file_title title,sc_ass_content cont,sc_date date,sc_people people from sys_casualties where del_flag = '"+ YNEnum.N.toString() +"' and sc_relation='"+vo.getId()+"' and mark ='"+ vo.getStatus() +"' " +
                " union all select ud_id id,ud_title title,ud_content cont,ud_date date,ud_people people from sys_urgencysettle_data where del_flag = '"+ YNEnum.N.toString() +"' and ud_relation='"+vo.getId()+"' and status ='"+ vo.getStatus() +"' ) a order by cont,date desc";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(CasAndUrgen.class));
    }

    @Override
    public void submit(CasVo vo) {
        String sql ="";
        if ("需紧急安置人员".equals(vo.getCont())){
            sql = "update sys_urgencysettle_data set" +
                    " status = '"+YNEnum.Y+"' " +
                    " where ud_id = '" + vo.getId() + "'";
        }else {
            sql= "update sys_casualties  set mark ='"+YNEnum.Y.toString()+"' where sc_id = '"+vo.getId()+"'";
        }
        jdbcTemplate.update(sql);
    }

    @Override
    public void remove(CasVo vo) {
        String sql ="";
        if ("需紧急安置人员".equals(vo.getCont())){
            sql = "update sys_urgencysettle_data set" +
                    " status = '"+YNEnum.N+"' " +
                    " where ud_id = '" + vo.getId() + "'";
        }else {
            sql= "update sys_casualties  set mark ='"+YNEnum.N.toString()+"' where sc_id = '"+vo.getId()+"'";
        }
        jdbcTemplate.update(sql);

    }


}
